package org.docmanagement.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts.Globals;
import org.apache.struts.taglib.TagUtils;
import org.docmanagement.forms.ElementForm;

/**
 * Created on 13 nov. 2004
 * @author eveno
 */
public class TypeLabelTag extends TagSupport
{
	private static final long serialVersionUID = 1L;

    private String beanName = null;
    private String property = null;
    private String scope = "request";
    /**
     * 
     */
    public TypeLabelTag()
    {
        super();
    }

    public String getName()
    {
        return beanName;
    }

    public void setName(String n)
    {
        this.beanName = n;
    }

    public String getProperty()
    {
        return property;
    }

    public void setProperty(String pProperty)
    {
        this.property = pProperty;
    }
    /**
     * @return Returns the scope.
     */
    public String getScope()
    {
        return scope;
    }
    /**
     * @param scope The scope to set.
     */
    public void setScope(String scope)
    {
        this.scope = scope;
    }

    public int doStartTag() throws JspException
    {
        return SKIP_BODY;
    }

    public int doEndTag() throws JspException
    {
        TagUtils tagUtils = TagUtils.getInstance();
        char type = 0;
        if (property == null)
        {
	        Object o = tagUtils.lookup(pageContext, beanName, scope);
	        if (!(o instanceof ElementForm) )
	            throw new JspException(
	                    tagUtils.message(
	                            pageContext,
	                            Globals.MESSAGES_KEY,
	                            Globals.LOCALE_KEY,
	                            "error.bean.wrongtype",
	                            new Object[] {
	                                    beanName,
	                                    ElementForm.class.getName()
	                            }
	                    )
	            );
	        type = ((ElementForm)o).getType();
		}
		else
		{
	        Object o = tagUtils.lookup(pageContext, beanName, property, scope);
	        type = ((Character) o).charValue();
		}
	
        String label = tagUtils.message(
                pageContext,
                Globals.MESSAGES_KEY,
                Globals.LOCALE_KEY,
                getLabelKey(type)
        );
        tagUtils.write(pageContext, label);
        return EVAL_PAGE;
    }

    /**
     * @param type
     * @return
     */
    private String getLabelKey(char type)
    {
        switch (type)
        {
            case 'F':
                return "file.type.label";
            case 'L':
                return "link.type.label";
            case 'B':
                return "book.type.label";
            default:
                return null;
        }
    }

    public void release()
    {
        super.release();
        beanName = null;
        property = null;
    }
}
